//
//  ChatInputView.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/12.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "ChatInputView.h"
#import "Common.h"
#import "SVProgressHUD.h"

@interface ChatInputView ()<UITextViewDelegate>

@property (nonatomic, strong)UIButton *sendBtn;
@property (nonatomic, strong)UIButton *voiceBtn;

@end

@implementation ChatInputView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        self.backgroundColor = [UIColor whiteColor];
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.bgView];
        
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 0.3)];
        lineView.backgroundColor = [UIColor grayColor];
        lineView.alpha = 0.3;
        [self.bgView addSubview:lineView];
        
        
        UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(5, 8, 40, 40)];
        [btn1 setImage:[UIImage imageNamed:@"chatVoice"] forState:UIControlStateNormal];
        [btn1 setImage:[UIImage imageNamed:@"chatText"] forState:UIControlStateSelected];

        [self addSubview:btn1];
        [btn1 addTarget:self action:@selector(voiceMethod:) forControlEvents:UIControlEventTouchUpInside];
        
//
//        self.emjonBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-90,8 , 40, 40)];
//        [self.emjonBtn setImage:[UIImage imageNamed:@"chatemotion"] forState:UIControlStateNormal];
//        [self.bgView addSubview:self.emjonBtn];
//        self.emjonBtn.tag = 99;
//        [self.emjonBtn addTarget:self action:@selector(clickAdd:) forControlEvents:UIControlEventTouchUpInside];
        
        self.addBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-45, 8, 40,40)];
        [self.addBtn setImage:[UIImage imageNamed:@"chatAdd"] forState:UIControlStateNormal];
        [self.bgView addSubview:self.addBtn];
         self.addBtn.tag = 100;
        [self.addBtn addTarget:self action:@selector(clickAdd:) forControlEvents:UIControlEventTouchUpInside];
        
  
        
//        self.chatText = [[UITextView alloc]initWithFrame:CGRectMake(45, 10, SCREENW-140, frame.size.height-20)];
          self.chatText = [[UITextView alloc]initWithFrame:CGRectMake(45, 10, SCREENW-140, 35)];
        self.chatText.textColor = bgmsmallColor;
        self.chatText.returnKeyType = UIReturnKeySend;
        self.chatText.font = [UIFont systemFontOfSize:15];
        self.chatText.delegate = self;
        [self.bgView addSubview:self.chatText];
        
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 35)];
        self.label.text = @" 输入你想说的话";
        self.label.textColor = bgmsmallColor;
        self.label.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.label];
        
        self.voiceBtn = [[UIButton alloc]initWithFrame:self.chatText.frame];
        [self.voiceBtn setTitle:@"按住 说话" forState:UIControlStateNormal];
        [self.voiceBtn setTitleColor:bgmsmallColor forState:UIControlStateNormal];
        [self.bgView addSubview:self.voiceBtn];
        self.voiceBtn.hidden = YES;
        
        [self.voiceBtn addTarget:self action:@selector(voiceButtonTouchBegin)forControlEvents:UIControlEventTouchDown];
        [self.voiceBtn addTarget:self action:@selector(voiceButtonTouchEnd)forControlEvents:UIControlEventTouchUpInside];
        [self.voiceBtn addTarget:self action:@selector(voiceButtonTouchEnd)forControlEvents:UIControlEventTouchUpOutside];
        
    }
    return self;
}

- (void)voiceMethod:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        self.label.hidden = YES;
        self.chatText.hidden = YES;
        self.voiceBtn.hidden = NO;
        [self.chatText resignFirstResponder];

    }else{
        self.label.hidden = NO;
        self.chatText.hidden = NO;
        self.voiceBtn.hidden = YES;

    }
}
- (void)voiceButtonTouchBegin{
    [self.voiceBtn setTitle:@"松开 结束" forState:UIControlStateNormal];
    self.voiceBtn.backgroundColor = bgmColor(244, 244, 244);
    [self.chatDelegate startVioce];
}
- (void)voiceButtonTouchEnd{
    
    self.voiceBtn.backgroundColor =[UIColor whiteColor];
    [self.voiceBtn setTitle:@"按住 说话" forState:UIControlStateNormal];
    [self.chatDelegate endVioce];
}
-(void)clickAdd:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    if (btn.tag==100) {
        if (btn.selected) {
            [self.chatDelegate showNomalView];
        }else{
            [self.chatDelegate hideNomalView];
        }
    }else if (btn.tag ==99){
        if (btn.selected) {
            [self.chatDelegate showEmjionView];
        }else{
            [self.chatDelegate hideEmjionView];
        }
    }
   
   
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.label.hidden = YES;
    
}
- (void)textViewDidChange:(UITextView *)textView{
    
    if(textView.text.length==0){
        self.label.hidden = NO;
    }else if (textView.text.length>0){
        self.label.hidden = YES;
    }
}

- (void)clickSend{
    if (self.chatText.text.length !=0) {
        [self.chatDelegate sendChatText:self.chatText.text];
        self.chatText.text = nil;

    }else{
        [SVProgressHUD showInfoWithStatus:@"输入你想说的话"];
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        self.chatText.text = textView.text;
        [self.chatDelegate sendChatText:textView.text];
        self.chatText.text = @"";
        self.label.hidden = NO;

        return NO;
    }
 
    return YES;
    
}


@end
